layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 货运订单明细表管理
     */
    var OrderTransportDetailed = {
        tableId: "orderTransportDetailedTable"
    };

    /**
     * 初始化表格的列
     */
    OrderTransportDetailed.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'goodsName', sort: true, title: 'Product Name', style:'background-color: #dbf1e1;font-weight: 600;',width:200, event: 'checkbox'},
            {field: 'goodsNumber', sort: true, title: 'Product Number', style:'background-color: #dbf1e1;font-weight: 600;', event: 'checkbox'},
            {field: 'goodsNo', sort: true, title: 'Item No.', style:'background-color: #dbf1e1;font-weight: 600;', event: 'checkbox', totalRowText: 'Total: '},
            {field: 'unitprice', sort: true, title: 'Unit Price'},
            {field: 'transportMoney', sort: true, title: 'Amount', totalRow: true},
            {field: 'batchnumber', sort: true, title: 'Batch'},
            {field: 'notnum', sort: true, title: 'Quantity', totalRow: true, style:'font-weight: 600;', event: 'checkbox'},
            {field: 'notvolume', sort: true, title: 'Volume', totalRow: true, style:'font-weight: 600;', event: 'checkbox'},
            {field: 'shuruVolume', sort: true, title: 'Configured Volume', totalRow: true, style:'font-weight: 600;', event: 'checkbox'},
            {field: 'shurunum', sort: true, title: 'Configured Quantity', totalRow: true, style:'font-weight: 600;color: #2b85e4;text-decoration:underline;',edit:'text'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    OrderTransportDetailed.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['goodsName'] = $("#goodsName").val();
        table.reload(OrderTransportDetailed.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OrderTransportDetailed.tableId,
        url: Feng.ctxPath + '/orderTransportDispatch/findListBySpecWeiPei?transportNumber='+Feng.getUrlParam("transportNumber"),
        page: false,
        limit: 1000,
        height: "full-60",
        totalRow: true,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        cellMinWidth: 100,
        cols: OrderTransportDetailed.initColumn()
    });

    // 编辑单元格 事件
    table.on('edit(' + OrderTransportDetailed.tableId + ')', function (obj) {
        var data = obj.data;//得到所在行所有键值
		var value = obj.value;//修改后的值
		if(typeof parseFloat(value) != 'number' ||  parseFloat(value) <= 0){
    		layer.msg('Please enter a valid number'); 
			return
		}
		if(parseFloat(value) > parseFloat(data.notnum)){
    		layer.msg('Exceeds order quantity'); 
			return
		}
		data.shuruVolume  = (value*data.goodsVolume).toFixed(3)//计算配置体积
		obj.update(data)//更新编辑行
		
		table.reload('orderTransportDetailedTable',{//重新渲染表格
			url:'',
			data:table.cache.orderTransportDetailedTable
		})		
		
		var volume = 0
		var data = table.checkStatus(OrderTransportDetailed.tableId).data
		for (var i = 0; i < data.length; i++) {
			volume = (parseFloat(volume)+parseFloat(data[i].shuruVolume)).toFixed(3)
		}
		$("#volume").text(volume)
    });

	//选择订单明细
	table.on('checkbox(' + OrderTransportDetailed.tableId + ')', function(obj){
		if(obj.checked){
			if(obj.type == 'all'){
				var volume = 0
				var data = table.checkStatus(OrderTransportDetailed.tableId).data
				for (var i = 0; i < data.length; i++) {
					volume = (parseFloat(volume)+parseFloat(data[i].shuruVolume)).toFixed(3)
				}
				$("#volume").text(volume)
			} else if (obj.type == 'one'){
				var volume = 0
				var data = table.checkStatus(OrderTransportDetailed.tableId).data
				for (var i = 0; i < data.length; i++) {
					volume = (parseFloat(volume)+parseFloat(data[i].shuruVolume)).toFixed(3)
				}
				$("#volume").text(volume)
			}
		} else {
			if(obj.type == 'all'){
				$("#volume").text("0")
			} else if (obj.type == 'one'){
				var volume = 0
				var data = table.checkStatus(OrderTransportDetailed.tableId).data
				for (var i = 0; i < data.length; i++) {
					volume = (parseFloat(volume)+parseFloat(data[i].shuruVolume)).toFixed(3)
				}
				$("#volume").text(volume)
			}
		}
	 });

     // 确定
     $('#queding').click(function () {
     	var checkStatus = table.checkStatus('orderTransportDetailedTable');
        data = checkStatus.data;
        if (data.length>0) {
			var new_list2 = []
			var mingxiorder = ''
            for (var i = 0; i < data.length; i++) {
				if(data[i].shurunum == ''){
    				layer.msg('Please enter a number'); 
					return
				}
				if(typeof parseFloat(data[i].shurunum) != 'number' ||  parseFloat(data[i].shurunum) <= 0){
    				layer.msg('Please enter a valid number'); 
					return
				}
				if(parseFloat(data[i].shurunum) > parseFloat(data[i].notnum)){
    				layer.msg('Exceeds order quantity'); 
					return
				}
				if(mingxiorder == ''){
					mingxiorder = data[i].transportNumber
				}
				let obj = {}
				obj.num = data[i].shurunum
				obj.transportNumber = data[i].transportNumber
				obj.goodId = data[i].goodId
				obj.batchnumber = data[i].batchnumber
				obj.totalVolume = (data[i].shurunum*data[i].goodsVolume).toFixed(3)
				new_list2.push(obj)
			}
            for (var i = 0; i < parent.list2.length; i++) {
				if(parent.list2[i].transportNumber != mingxiorder){
					new_list2.push(parent.list2[i])
				}
			}
			parent.list2 = new_list2
	     	var Index = parent.layer.getFrameIndex(window.name);
	        parent.layer.close(Index);
		} else {
			layer.alert('Please select at least one product');
		}
     });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OrderTransportDetailed.search();
    });

    // 单击行事件
    table.on('row(' + OrderTransportDetailed.tableId + ')', function (obj) {
		$('div[lay-id="' + OrderTransportDetailed.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + OrderTransportDetailed.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });

});
